import 'package:aichat_flutter_wallet/module/module_browser/viewmodel/before_browser.dart';
import 'package:aichat_flutter_wallet/module/module_dapp/widgets/components/dapp_tip_com.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/image/public_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../../module_common/widgets/header/page_header.dart';
import 'package:aichat_flutter_wallet/module/module_dapp/provider/dapp_provider.dart';
import '../utils/dapp_favorite_util.dart';
import '../widgets/hover_status.dart';
import 'package:lib_database/src/bean.dart';
import 'package:provider/provider.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/page_status_com.dart';

class DAppFavoritePage extends StatefulWidget {
  const DAppFavoritePage({super.key, required this.title});

  final String title;

  @override
  State<DAppFavoritePage> createState() => _DAppFavoritePageState();
}

class _DAppFavoritePageState extends State<DAppFavoritePage> {
  Widget createBodyLayout(
      BuildContext context, List<DAppBean> favoriteDAppList) {
    return ListView.builder(
      padding: EdgeInsets.all(0),
      itemCount: favoriteDAppList.length + 1,
      itemBuilder: (BuildContext buildContext, int index) {
        if (index < favoriteDAppList.length) {
          return HoverStatus(
            defaultColor: Color.fromRGBO(255, 255, 255, 1),
            hoverColor: Color.fromRGBO(246, 246, 246, 1),
            onPressed: () {
              DAppBean item = favoriteDAppList[index];
              WebSiteInfoItem webSiteInfoItem = WebSiteInfoItem(
                  websiteUrl: item.url!,
                  websiteImg: item.logo!,
                  websiteName: item.name!,
                  websiteTag: item.statement!);
              doGotoBrowser(
                  context: context,
                  webUrl: item.url ?? '',
                  isSearchIn: false,
                  webSiteInfoItem: webSiteInfoItem);
            },
            child: Container(
                padding: EdgeInsets.only(left: 20.w),
                height: 70.w,
                child: Stack(
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Padding(
                          padding: EdgeInsets.only(right: 10.w),
                          child: ClipRRect(
                            borderRadius: BorderRadius.circular(46.w),
                            child: PublicImage(
                              image: favoriteDAppList[index].logo ?? '',
                              width: 46.w,
                              height: 46.w,
                            ),
                          ),
                        ),
                        Expanded(
                            child: Padding(
                          padding: EdgeInsets.only(right: 50.w),
                          child: Column(
                            mainAxisSize: MainAxisSize.max,
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Row(
                                children: [
                                  Container(
                                    constraints:
                                        BoxConstraints(maxWidth: 200.w),
                                    child: Text(
                                      favoriteDAppList[index].name!,
                                      overflow: TextOverflow.ellipsis,
                                      maxLines: 1,
                                      style: TextStyle(
                                        fontSize: 14.sp,
                                        height: 20.w / 14.w,
                                        fontWeight: FontWeight.w500,
                                        color: Color.fromRGBO(51, 51, 51, 1),
                                      ),
                                    ),
                                  ),
                                  SizedBox(
                                    width: 4,
                                  ),
                                  if (favoriteDAppList[index].statement != '')
                                    DappTips(
                                        title:
                                            favoriteDAppList[index].statement!)
                                  // Container(
                                  //   padding: EdgeInsets.all(3),
                                  //   decoration: const BoxDecoration(
                                  //     borderRadius: BorderRadius.only(
                                  //         topLeft: Radius.circular(7),
                                  //         topRight: Radius.circular(7),
                                  //         bottomRight: Radius.circular(7)),
                                  //     color: Colors.white,
                                  //     gradient: LinearGradient(
                                  //       begin: Alignment.topLeft,
                                  //       end: Alignment.bottomRight,
                                  //       colors: [
                                  //         Color.fromRGBO(185, 156, 255, 1),
                                  //         Color.fromRGBO(122, 88, 255, 1),
                                  //       ],
                                  //     ),
                                  //   ),
                                  //   child: Text(
                                  //     favoriteDAppList[index].statement!,
                                  //     style: TextStyle(
                                  //       fontSize: 8,
                                  //       height: 20 / 14,
                                  //       fontWeight: FontWeight.w500,
                                  //       color:
                                  //           Color.fromRGBO(255, 255, 255, 1),
                                  //     ),
                                  //   ),
                                  // )
                                ],
                              ),
                              SizedBox(
                                height: 3.w,
                              ),
                              Text(favoriteDAppList[index].url!,
                                  overflow: TextOverflow.ellipsis,
                                  maxLines: 1,
                                  style: TextStyle(
                                    fontSize: 12.sp,
                                    height: 17.w / 12.w,
                                    fontWeight: FontWeight.w400,
                                    color: Color.fromRGBO(153, 153, 153, 1),
                                  ))
                            ],
                          ),
                        ))
                      ],
                    ),
                    Positioned(
                      top: 0,
                      right: 14,
                      child: IgnorePointer(
                          ignoring: false,
                          child: GestureDetector(
                            onTap: () {
                              DAppFavoriteUtil.showSetFavorite(
                                context,
                                needToast: false,
                                DApp: DAppBean(
                                  favoriteDAppList[index].uid,
                                  favoriteDAppList[index].name,
                                  favoriteDAppList[index].logo,
                                  favoriteDAppList[index].url,
                                  '',
                                  favoriteDAppList[index].chains!,
                                  favoriteDAppList[index].statement!,
                                ),
                              );
                            },
                            child: Container(
                              width: 40.w,
                              height: 70.w,
                              color: Colors.transparent,
                              alignment: Alignment.center,
                              child: Image.asset(
                                'assets/images/dapp/star-active.png',
                                width: 16.w,
                                height: 16.w,
                                fit: BoxFit.fitWidth,
                              ),
                            ),
                          )),
                    )
                  ],
                )),
          );
        } else {
          return Container(
            height: 70.w,
            alignment: Alignment.center,
            child: Text(
              'Already at the bottom',
              style: TextStyle(
                  fontSize: 14.sp,
                  color: Color.fromRGBO(190, 189, 202, 1),
                  fontWeight: FontWeight.w400),
            ),
          );
        }
      },
    );
  }

  // ModalBottomSheet
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //body占满剩余空间 可滚动
      body: Consumer<DAppProvider>(builder: (c, model, topWidget) {
        final favoriteDAppList = model.favoriteDAppList;
        return Container(
            color: Color.fromRGBO(255, 255, 255, 1),
            child: Column(children: [
              PageHeaderCom(
                title: 'Favorites',
              ),
              Expanded(
                  flex: 1,
                  child: SizedBox(
                      child: favoriteDAppList.length == 0
                          ? PageEmptyCom(type: 'empty', paddingTop: 100)
                          : createBodyLayout(context, favoriteDAppList)))
            ]));
      }),
    );
  }
}
